var app = getApp()

Page({
    data: {
        item_id:'', 
    
        data: [],
        page: 1,         // 页码

        userData: "",         // 用户数据 openid token

        loading: false,      // 加载中
        noData: false,       // 没有数据
        noMore: false,       // 没有更多
        hint: false,         // 是否提示
    },
    getIndexList(res) {
        var self = this;
        var noData = false;                        // 是否没有数据
        var noMore = false;                        // 是否没有更多
        var page = this.data.page;
        var datas = this.data.data;            // 原数据
        var url = '/order/en_order_list';

        wx.showLoading({ title: '加载中' })

        if (page == 1) { datas = [] }
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        page: page
                    },
                    success: function (res) {
                        var list = res.data;

                        if ( list.data || datas.length ) { noData = 0 } else { noData = 1 }

                        self.setData({noData})

                        if (!list.hasNext) {
                            self.setData({ noMore: true })
                        }
                        
                        if (list.code == 1) {
                            self.setData({
                                data: datas.concat(list.data.data),
                                loading: false
                            })
                        } else {
                            if (resbanner.data.code == -1 || resbanner.data.code == 404) {
                                self.loginOverdue();
                            }
                        }

                        setTimeout(function () {
                            wx.stopPullDownRefresh()
                            wx.hideLoading()
                        }, 300)
                    }
                })
            },
        })
       
    },

    onLoad: function () {
        var that = this;

        this.login = this.selectComponent("#login");
        this.dialog = this.selectComponent("#dialog");                  // 未授权手机号提示

        this.getIndexList();
    },
    // 登录过期状态
    loginOverdue(e) {
        var self = this;
        wx.removeStorage({
            key: 'user',
            success: function (res) {
                self.onLoad();
            }
        })
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            data: [],
            page: 1
        })
        this.onLoad();
    },
    // 加载更多
    onReachBottom() {
        var page = ++this.data.page;
        var noMore = this.data.noMore;
        var loading = this.data.loading;
        var userData = this.data.userData;

        if (!noMore && !loading) {
            this.setData({ page: page, loading: true })
            wx.showLoading({ title: '加载中' })
            this.getIndexList(userData);
        } else {
            if (!this.data.hint) {
                wx.showToast({
                    image: '../../img/warning.png',
                    title: '暂无更多',
                })
                this.setData({ hint: true })
            }
        }
    },
    // 显示确认收货弹窗
    __showDialog(e){
        this.setData({
            item_id : e.target.dataset.id 
        })
        this.dialog.showDialog();
    },
    // 确认收货 - 确认按钮
    __confirmEvent(){
        this.confirmReceiving();
        this.dialog.hideDialog();
    },
    // 确认收货 - 取消按钮
    __cancelEvent() {
        this.dialog.hideDialog();
    },
    // 确认收货
    confirmReceiving() {
        var self = this;
        var url = "/order/buy_confirmreceive";
        var id = this.data.item_id;

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id
                    },
                    success: function (res) {
                        var list = res.data;
                        if (list.code == 1) {
                            
                        }
                    }
                })
            },
        })
    },
})